import React, { createContext, useContext } from 'react';

export interface OptionItem {
    text: string;
    value?: string | number;
}

export interface MenuItemProps {
    disabled?: boolean;
    // 子项索引
    itemIndex?: number;
    defaultOption?: OptionItem;
    // 是否显示子项
    showItem?: boolean;
    options?: OptionItem[];
    onChange?: (event: any) => void;
}

export type MenuCallback = (index: number) => void;

export interface MenuContextProps {
    showMenuItem: boolean[];
    activeIndex: number;
    toggleMenuHandler: (value: {index: number, options?: OptionItem[], defaultOption?: OptionItem}) => void;
    updateTitle: (index: number, title: string) => void;
    hideMenuHandler: (index: number) => void;
}

export const MenuContext = createContext<MenuContextProps | null>(null);

export const useMenu = () => {
    const context = useContext(MenuContext)
    if (!context) throw new Error('useMenu must be used within MenuProvider')
    return context
}